| Conditions | 1 |
| Paths | 4 |
| Total Lines | 159 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global API */ |
||
| 36 | .controller('EditCtrl', ['$scope', '$routeParams', '$timeout', 'notify', function ($scope, $routeParams, $timeout, notify) { |
||
| 37 | API.runtime.sendMessage(API.runtime.id, { |
||
| 38 | method: "getCredentialByGuid", |
||
| 39 | args: $routeParams.guid |
||
| 40 | }).then(function (credential) { |
||
| 41 | $scope.canEdit = true; |
||
| 42 | if(credential.hasOwnProperty('acl')) { |
||
| 43 | var permissions = new SharingACL(credential.acl.permissions.permission); |
||
|
|
|||
| 44 | $scope.canEdit = permissions.hasPermission(0x02); |
||
| 45 | } |
||
| 46 | $scope.credential = credential; |
||
| 47 | $scope.credential.password_repeat = angular.copy(credential.password); |
||
| 48 | $scope.$apply(); |
||
| 49 | }); |
||
| 50 | |||
| 51 | var storage = new API.Storage(); |
||
| 52 | |||
| 53 | $scope.tabActive = 1; |
||
| 54 | |||
| 55 | function genPwd(settings) { |
||
| 56 | /* jshint ignore:start */ |
||
| 57 | var password = generatePassword(settings['length'], |
||
| 58 | settings.useUppercase, |
||
| 59 | settings.useLowercase, |
||
| 60 | settings.useDigits, |
||
| 61 | settings.useSpecialChars, |
||
| 62 | settings.minimumDigitCount, |
||
| 63 | settings.avoidAmbiguousCharacters, |
||
| 64 | settings.requireEveryCharType); |
||
| 65 | /* jshint ignore:end */ |
||
| 66 | return password; |
||
| 67 | } |
||
| 68 | |||
| 69 | $scope.pw_settings = null; |
||
| 70 | function getPasswordGenerationSettings(cb) { |
||
| 71 | var default_settings = { |
||
| 72 | 'length': 12, |
||
| 73 | 'useUppercase': true, |
||
| 74 | 'useLowercase': true, |
||
| 75 | 'useDigits': true, |
||
| 76 | 'useSpecialChars': true, |
||
| 77 | 'minimumDigitCount': 3, |
||
| 78 | 'avoidAmbiguousCharacters': false, |
||
| 79 | 'requireEveryCharType': true |
||
| 80 | }; |
||
| 81 | storage.get('password_generator_settings').then(function (_settings) { |
||
| 82 | if (!_settings) { |
||
| 83 | _settings = default_settings; |
||
| 84 | } |
||
| 85 | |||
| 86 | $scope.pw_settings = _settings; |
||
| 87 | }).error(function () { |
||
| 88 | $scope.pw_settings = default_settings; |
||
| 89 | }); |
||
| 90 | } |
||
| 91 | |||
| 92 | getPasswordGenerationSettings(); |
||
| 93 | |||
| 94 | var custom_field = { |
||
| 95 | label: '', |
||
| 96 | value: '', |
||
| 97 | field_type: 'text', |
||
| 98 | secret: false |
||
| 99 | }; |
||
| 100 | |||
| 101 | $scope.new_custom_field = angular.copy(custom_field); |
||
| 102 | |||
| 103 | $scope.addCustomField = function (_field) { |
||
| 104 | var field = angular.copy(_field); |
||
| 105 | if (!field.label || !field.value) { |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | $scope.credential.custom_fields.push(field); |
||
| 109 | $scope.new_custom_field = angular.copy(custom_field); |
||
| 110 | }; |
||
| 111 | |||
| 112 | $scope.deleteCustomField = function (field) { |
||
| 113 | var idx = $scope.credential.custom_fields.indexOf(field); |
||
| 114 | $scope.credential.custom_fields.splice(idx, 1); |
||
| 115 | }; |
||
| 116 | |||
| 117 | $scope.pwFieldShown = false; |
||
| 118 | |||
| 119 | $scope.togglePwField = function () { |
||
| 120 | $scope.pwFieldShown = !$scope.pwFieldShown; |
||
| 121 | }; |
||
| 122 | |||
| 123 | var round = 0; |
||
| 124 | $scope.generatePassword = function () { |
||
| 125 | var new_password = genPwd($scope.pw_settings); |
||
| 126 | $scope.credential.password = new_password; |
||
| 127 | $scope.credential.password_repeat = new_password; |
||
| 128 | $timeout(function () { |
||
| 129 | if (round < 10) { |
||
| 130 | $scope.generatePassword(); |
||
| 131 | round++; |
||
| 132 | } else { |
||
| 133 | round = 0; |
||
| 134 | } |
||
| 135 | }, 10); |
||
| 136 | }; |
||
| 137 | $scope.saving = false; |
||
| 138 | $scope.saveCredential = function () { |
||
| 139 | if(!$scope.canEdit){ |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | $scope.saving = true; |
||
| 143 | if (!$scope.credential.label) { |
||
| 144 | notify(API.i18n.getMessage('label_required')); |
||
| 145 | return; |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($scope.credential.password !== $scope.credential.password_repeat) { |
||
| 149 | notify(API.i18n.getMessage('no_password_match')); |
||
| 150 | return; |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($scope.new_custom_field.label && $scope.new_custom_field.value) { |
||
| 154 | $scope.credential.custom_fields.push(angular.copy($scope.new_custom_field)); |
||
| 155 | } |
||
| 156 | delete $scope.credential.password_repeat; |
||
| 157 | |||
| 158 | API.runtime.sendMessage(API.runtime.id, { |
||
| 159 | method: "saveCredential", |
||
| 160 | args: $scope.credential |
||
| 161 | }).then(function () { |
||
| 162 | $scope.saving = false; |
||
| 163 | if (!$scope.credential.credential_id) { |
||
| 164 | notify(API.i18n.getMessage('credential_created')); |
||
| 165 | } else { |
||
| 166 | notify(API.i18n.getMessage('credential_updated')); |
||
| 167 | } |
||
| 168 | window.location = '#!/'; |
||
| 169 | }); |
||
| 170 | |||
| 171 | }; |
||
| 172 | |||
| 173 | $scope.deleteCredential = function () { |
||
| 174 | $scope.credential.delete_time = new Date().getTime() / 1000; |
||
| 175 | API.runtime.sendMessage(API.runtime.id, { |
||
| 176 | method: "saveCredential", |
||
| 177 | args: $scope.credential |
||
| 178 | }).then(function () { |
||
| 179 | notify(API.i18n.getMessage('credential_deleted')); |
||
| 180 | API.runtime.sendMessage(API.runtime.id, {method: "getCredentials"}).then(function () { |
||
| 181 | setTimeout(function () { |
||
| 182 | window.location = '#!/'; |
||
| 183 | }, 1900); |
||
| 184 | }); |
||
| 185 | |||
| 186 | }); |
||
| 187 | }; |
||
| 188 | |||
| 189 | $scope.cancel = function () { |
||
| 190 | window.location = '#!/'; |
||
| 191 | }; |
||
| 192 | |||
| 193 | |||
| 194 | }]); |
||
| 195 | }()); |
||
| 196 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.